import 'package:elevator/model/signal_info.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../../../../../../model/build_info.dart';
import '../../../../../../../../model/lift_info.dart';
import 'first_logic.dart';

class FirstPage extends StatefulWidget {

  late FirstLogic logic;
  FirstPage(BuildInfo buildInfo,LiftInfo liftInfo, {Key? key}) : super(key: key){
    Get.delete<FirstLogic>();
    logic = Get.put(FirstLogic(buildInfo,liftInfo));
  }


  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _FirstPageState(logic);
  }

}

class _FirstPageState extends State<StatefulWidget>{
  late Size size;
  double itemWidth = 60;
  late FirstLogic logic;


  _FirstPageState(this.logic);

  @override
  Widget build(BuildContext context) {
    size = MediaQuery
        .of(context)
        .size;
    itemWidth = (size.width - 12 * 2) / 3;
    return Padding(padding: const EdgeInsets.all(10),
      child: SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: Container(
              color: Colors.white,
              child: Obx(() { return DataTable(
                  horizontalMargin:0,
                  columnSpacing: 0,
                  border: TableBorder.all(width: 1, color: Colors.grey),
                  headingRowHeight:0,
                  columns: const [
                    DataColumn(
                        numeric: true,
                        tooltip: '2',
                        label: Text('VUP')),
                    DataColumn(
                      numeric: true,
                      tooltip: '2',
                      label: Text('VDN'),
                    ),
                    DataColumn(
                      label: Text('VSND2'),
                    ),
                    DataColumn(
                      label: Text('INS(Maintenance)'),
                    ),
                    DataColumn(
                      label: Text('SFD(a)\n(Fault)'),
                    ),
                    DataColumn(
                      label: Text('SFD(a)\n(Fault)'),
                    ),
                    DataColumn(
                      label: Text('SFD(a)\n(Fault)'),
                    ),
                    DataColumn(
                      label: Text('SFD(a)\n(Fault)'),
                    ),
                  ], rows: loadSignalInfos(logic.signalInfoList));
              }))
      ),);
  }

  List<DataRow> loadSignalInfos(List<SignalInfo> signs) {
    List<DataRow> dataRows = [];
    if(signs.isNotEmpty){
      for (int i = 0; i < signs.length; i++) {
        dataRows.add(DataRow(
          cells: [
            DataCell(buildDataCell(signs[i].bit7,signs[i].bit7bg)),
            DataCell(buildDataCell(signs[i].bit6,signs[i].bit6bg)),
            DataCell(buildDataCell(signs[i].bit5,signs[i].bit5bg)),
            DataCell(buildDataCell(signs[i].bit4,signs[i].bit4bg)),
            DataCell(buildDataCell(signs[i].bit3,signs[i].bit3bg)),
            DataCell(buildDataCell(signs[i].bit2,signs[i].bit2bg)),
            DataCell(buildDataCell(signs[i].bit1,signs[i].bit1bg)),
            DataCell(buildDataCell(signs[i].bit0,signs[i].bit0bg)),
          ],
        ));
      }
    }
    return dataRows;
  }

  buildDataCell(String label,Color? bgColor){
    return SizedBox.expand(
      child: Container(
        alignment:Alignment.center,
        color: bgColor,
        padding: const EdgeInsets.all(4),
        child: Text(label),
      ),
    );
  }

  @override
  void dispose() {
    logic.unregister();
    super.dispose();
  }

  @override
  void initState() {
    logic.register();
    super.initState();
  }
}